/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.home.store;

import java.util.*;





import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

import com.zll.framework.model.HashBox;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;
import com.qbooks.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class ReviewSaveCommand implements Command
{

    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	JSONObject json = new JSONObject();

    	HashBox box 		= new HashBox(request);

    	String memberId		= StringUtil.nvl(request.getSession().getAttribute("session.user_id"));
    	String memberName	= StringUtil.nvl(request.getSession().getAttribute("session.user_name"));

    	if (memberId.equals("")) {
    		json.put("result", 	false);
    		json.put("message",	"로그인이 필요합니다.");
    	}
    	else {
    		StoreDAO dao 		= new StoreDAO();

    		box.put("member_id", memberId);
    		box.put("member_name", memberName);

    		int count = dao.selectBookReviewCount(box.getParam()).getInt("count",0);
    		
    		if (count > 0) {
    			json.put("result", 	false);
        		json.put("message",	"이미 리뷰를 작성하셨습니다.");
    		} else {
    			dao.insertBookReview(box);
    			json.put("result", 	true);
    		}
    	}

    	
    	request.setAttribute("jsonData", json.toString());

    	return "/common/json_view.jsp";
    }
}
